﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Autodesk.AutoCAD.Geometry;
using System.Xml;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using WlanDesign;
using Autodesk.AutoCAD.Interop;
using System.Runtime.InteropServices;

namespace WlanDesignDll
{
    public partial class FormDrawingExport : Form
    {
        private Point3d pStart;
        private Point3d pEnd;
        public FormDrawingExport(Point3d point1, Point3d point2)
        {
            this.pStart = point1;
            this.pEnd = point2;
            InitializeComponent();
        }

        private void FormDrawingExport_Load(object sender, EventArgs e)
        {

        }

        private void btn_Do_Click(object sender, EventArgs e)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            string floor = this.tBox_Floor.Text;
            if (floor == "")
            {
                MessageBox.Show("楼层不能为空！");
                return;
            }
            // ini文件与当前文档在同一目录下，并且名字相同
            //string[] name = acDoc.Name.Split('.');
            //string iniName = name[0] + ".ini";
            string[] name1 = doc.Name.Split('.');
            string iniName1 = name1[0] + "子.ini";
            // 写入ini文件
            WriteIniTxt(iniName1, "floor", "building_floor", floor);
            WriteIniTxt(iniName1, "area", "building_start", this.pStart.ToString());
            WriteIniTxt(iniName1, "area", "building_end", this.pEnd.ToString());
            AcadApplication acAppComObj = null;
            const string strProgId = "AutoCAD.Application.18";
            // Get a running instance of AutoCAD
            try
            {
                acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
            }
            catch // An error occurs if no instance is running
            {
                try
                {
                    // Create a new instance of AutoCAD
                    acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);

                }
                catch
                {
                    // If an instance of AutoCAD is not created then message and exit
                    System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" + " could not be created.");
                    return;
                }
            }
            AcadDocument acDocComObj;

            acDocComObj = acAppComObj.ActiveDocument;
            acDocComObj.SendCommand("DrawingExport ");
            this.Close();
        }
        //
        // Summery
        // 加载信息到ini文件
        //
        public void WriteIniTxt(string sPath, string Section, string Key, string value)
        {
            string ServerName = value;

            IniFileRW ini = new IniFileRW(sPath);

            ini.IniWriteValue(Section, Key, ServerName);
        }
        
    }
}
